<?php

/**
 * This is the model class for table "tbl_business".
 *
 * The followings are the available columns in table 'tbl_business':
 * @property integer $id
 * @property string $businessGoal
 * @property string $businessStatus
 * @property integer $businessType_id
 * @property integer $industryGroup_id
 * @property integer $businessSector_id
 * @property integer $economicSector_id
 *
 * The followings are the available model relations:
 * @property Businesstype $businessType
 * @property Businesssector $businessSector
 * @property Economicsector $economicSector
 * @property Industrygroup $industryGroup
 * @property BusinessCase[] $businessCases
 * @property Businessresources[] $tblBusinessresources
 * @property Valueproposition[] $tblValuepropositions
 */
class Business extends CActiveRecord
{
	const BUSINESSGOAL_GETRICH = 'Get rich';
	const BUSINESSGOAL_GOODLIVING = 'Make a good living';
	const BUSINESSGOAL_BUILDAGREATPRODUCT = 'Build a great product';
	const BUSINESSGOAL_CHANGETHEWORLD = 'Change the world';
	const BUSINESSGOAL_GETAQUICKFLIP = 'Get a quick flip';
	
	const BUSINESSSTATUS_CONCEPT = 'Concept';
	const BUSINESSSTATUS_STARTUP = 'Start Up';
	const BUSINESSSTATUS_EXPANSION = 'Expansion';
	const BUSINESSSTATUS_MATUREBUSINESS = 'Mature business';
	const BUSINESSSTATUS_SATURATIODECLINE = 'Saturation & Decline';
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return Business the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_business';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('industryGroup_id, businessSector_id, economicSector_id', 'required'),
			array('businessType_id, industryGroup_id, businessSector_id, economicSector_id', 'numerical', 'integerOnly'=>true),
			array('businessGoal, businessStatus', 'length', 'max'=>256),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, businessGoal, businessStatus, businessType_id, industryGroup_id, businessSector_id, economicSector_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'businessType' => array(self::BELONGS_TO, 'Businesstype', 'businessType_id'),
			'businessSector' => array(self::BELONGS_TO, 'Businesssector', 'businessSector_id'),
			'economicSector' => array(self::BELONGS_TO, 'Economicsector', 'economicSector_id'),
			'industryGroup' => array(self::BELONGS_TO, 'Industrygroup', 'industryGroup_id'),
			'businessCases' => array(self::HAS_MANY, 'BusinessCase', 'business_id'),
			'tblBusinessresources' => array(self::MANY_MANY, 'Businessresources', 'tbl_business_businessResources(business_id, businessResources_id)'),
			'tblValuepropositions' => array(self::MANY_MANY, 'Valueproposition', 'tbl_business_valueProposition(business_id, valueProposition_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'businessGoal' => 'Business Goal',
			'businessStatus' => 'Business Status',
			'businessType_id' => 'Business Type',
			'industryGroup_id' => 'Industry Group',
			'businessSector_id' => 'Business Sector',
			'economicSector_id' => 'Economic Sector',
			'tblBusinessresources' => 'Business Resources',
			'tblValuepropositions' => 'Value Proposition',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('businessGoal',$this->businessGoal,true);
		$criteria->compare('businessStatus',$this->businessStatus,true);
		$criteria->compare('businessType_id',$this->businessType_id);
		$criteria->compare('industryGroup_id',$this->industryGroup_id);
		$criteria->compare('businessSector_id',$this->businessSector_id);
		$criteria->compare('economicSector_id',$this->economicSector_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function searchSameModel()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		$criteria->compare('businessGoal',$this->businessGoal,true);
		$criteria->compare('businessStatus',$this->businessStatus,true);
		$criteria->compare('valueProposition',$this->valueProposition,true);
		$criteria->compare('businessResources',$this->businessResources,true);
		$criteria->compare('industryGroup_id',$this->industryGroup_id,true);
		$criteria->compare('businessSector_id',$this->businessSector_id,true);
		$criteria->compare('economicSector_id',$this->economicSector_id,true);

		$businessArray = Business::model()->findAll($criteria);
		if($businessArray == null){
			
			$criteria=new CDbCriteria;
		
			$criteria->compare('businessGoal',$this->businessGoal,true);
			$criteria->compare('businessStatus',$this->businessStatus,true);
			$criteria->compare('valueProposition',$this->valueProposition,true);
			$criteria->compare('businessResources',$this->businessResources,true);
			$criteria->compare('businessSector_id',$this->businessSector_id,true);
			$criteria->compare('economicSector_id',$this->economicSector_id,true);
			
			$businessArray = Business::model()->findAll($criteria);
		
			if($businessArray == null){
				$criteria=new CDbCriteria;
		
				$criteria->compare('businessGoal',$this->businessGoal,true);
				$criteria->compare('businessStatus',$this->businessStatus,true);
				$criteria->compare('valueProposition',$this->valueProposition,true);
				$criteria->compare('businessResources',$this->businessResources,true);
				$criteria->compare('economicSector_id',$this->economicSector_id,true);
			
				$businessArray = Business::model()->findAll($criteria);
			
				if($businessArray == null){
					$criteria=new CDbCriteria;
		
					$criteria->compare('businessGoal',$this->businessGoal,true);
					$criteria->compare('businessStatus',$this->businessStatus,true);
					$criteria->compare('valueProposition',$this->valueProposition,true);
					$criteria->compare('businessResources',$this->businessResources,true);
					
					$businessArray = Business::model()->findAll($criteria);
				
					
				if($businessArray == null){
					$criteria=new CDbCriteria;
		
					$criteria->compare('businessGoal',$this->businessGoal,true);
					$criteria->compare('businessStatus',$this->businessStatus,true);
					$criteria->compare('valueProposition',$this->valueProposition,true);
					$criteria->compare('industryGroup_id',$this->industryGroup_id,true);
					$criteria->compare('businessSector_id',$this->businessSector_id,true);
					$criteria->compare('economicSector_id',$this->economicSector_id,true);
			
					$businessArray = Business::model()->findAll($criteria);
				if($businessArray == null){
					$criteria=new CDbCriteria;
		
					$criteria->compare('businessGoal',$this->businessGoal,true);
					$criteria->compare('businessStatus',$this->businessStatus,true);
					$criteria->compare('valueProposition',$this->valueProposition,true);
					$criteria->compare('businessSector_id',$this->businessSector_id,true);
					$criteria->compare('economicSector_id',$this->economicSector_id,true);
			
					$businessArray = Business::model()->findAll($criteria);
				if($businessArray == null){
					$criteria=new CDbCriteria;
		
					$criteria->compare('businessGoal',$this->businessGoal,true);
					$criteria->compare('businessStatus',$this->businessStatus,true);
					$criteria->compare('valueProposition',$this->valueProposition,true);
					$criteria->compare('economicSector_id',$this->economicSector_id,true);
			
					$businessArray = Business::model()->findAll($criteria);
				if($businessArray == null){
					$criteria=new CDbCriteria;
		
					$criteria->compare('businessGoal',$this->businessGoal,true);
					$criteria->compare('businessStatus',$this->businessStatus,true);
					$criteria->compare('valueProposition',$this->valueProposition,true);		
			
					$businessArray = Business::model()->findAll($criteria);
				}
				}				
				}
				}}}
						
				
		}
		return $businessArray;
	}
	
public function getBGoalOptions()
	{
		 return array(
    		self::BUSINESSGOAL_GETRICH=>'Get rich',
    		self::BUSINESSGOAL_GOODLIVING=>'Make a good living',
    		self::BUSINESSGOAL_BUILDAGREATPRODUCT=>'Build a great product',
    		self::BUSINESSGOAL_CHANGETHEWORLD=>'Change the world',
    		self::BUSINESSGOAL_GETAQUICKFLIP=>'Get a quick flip',
		  );
	}
	
	public function getBStatusOptions()
	{
		 return array(
    		self::BUSINESSSTATUS_CONCEPT=>'Concept',
    		self::BUSINESSSTATUS_STARTUP=>'Start Up',
    		self::BUSINESSSTATUS_EXPANSION=>'Expansion',
    		self::BUSINESSSTATUS_MATUREBUSINESS=>'Mature business',
    		self::BUSINESSSTATUS_SATURATIODECLINE=>'Saturation & Decline',
		  );
	}

	public function getForm() {
		// correct businessResources

    	return new BusinessForm(array(
			'showErrorSummary'=>true,
			'attributes'=>array('id'=>'bcase'),
			'elements'=>array(
				'businessGoal'=>array(
					'type'=>'dropdownlist',
					'items'=>$this->getBGoalOptions(),
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				'businessStatus'=>array(
					'type'=>'dropdownlist',
					'items'=>$this->getBStatusOptions(),
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				'businessType_id'=>array(
					'type'=>'text',
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				/*
				'typeOfRights'=>array(
					'type'=>'dropdownlist',
					'items'=>CHtml::listData(TypeOfRights::model()->findAll(),'id','name'),
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				
				'typeOfAsset'=>array(
					'type'=>'dropdownlist',
					'items'=>CHtml::listData(TypeOfAsset::model()->findAll(),'id','name'),
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				*/
				'tblValuepropositions'=>array(
					'type'=>'checkboxlist',
					'items'=>CHtml::listData(ValueProposition::model()->findAll(), 'id', 'name'),
					'name'=>'valueProposition',
					'attributeitem' => 'id',
					'separator'=>'<br>',
					'labelOptions'=>array('style'=>'display:inline;'),					
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				
				'tblBusinessresources'=>array(
					'type'=>'checkboxlist',
					'items'=>CHtml::listData(BusinessResources::model()->findAll(), 'id', 'name'),
					'name'=>'businessResources',
					'attributeitem' => 'id',
					'separator'=>'<br>',
					'labelOptions'=>array('style'=>'display:inline;'),					
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				
				'economicSector_id'=>array(
					'type'=>'dropdownlist',
					'items'=>CHtml::listData(EconomicSector::model()->findAll(),'id','name'),
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				'businessSector_id'=>array(
					'type'=>'dropdownlist',
					'items'=>CHtml::listData(BusinessSector::model()->findAll(),'id','name'),
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
				
				'industryGroup_id'=>array(
					'type'=>'dropdownlist',
					'items'=>CHtml::listData(IndustryGroup::model()->findAll(),'id','name'),
					'layout'=>'{label}<br/>{input}<br/>{error}'
				),
			),
			
			'buttons'=>array(
				'submit'=>array(
					'type'=>'submit',
					'label'=>'Next'
				)
			)
		), $this);
	}
	
public function isRecordExists($business)
	{		/*
	// correct businessResources
		$sqlBusiness = 'SELECT tbl_business.id
			FROM tbl_business, tbl_businessResources
			WHERE tbl_business.businessGoal = :businessGoal AND
			tbl_business.productType = :productType AND
			tbl_business.businessStatus = :businessStatus AND
			tbl_business.valueProposition = :valueProposition AND
			tbl_business.competitors = :competitors AND
			tbl_business.industryGroup_id = :industryGroup_id AND
			tbl_business.businessSector_id = :businessSector_id AND
			tbl_business.economicSector_id = :economicSector_id';
		
		$dataBusiness=new CSqlDataProvider($sqlBusiness, array(
    		'params'=>array(':businessGoal'=>$business->businessGoal,
							':productType'=>$business->productType,
							':businessStatus'=>$business->businessStatus,
							':valueProposition'=>$business->valueProposition,
							':competitors'=>$business->competitors,
    						':industryGroup_id'=>$business->industryGroup_id,
    						':businessSector_id'=>$business->businessSector_id,
    						':economicSector_id'=>$business->economicSector_id),
    		'sort'=>array(
        		'attributes'=>array(
            		 'id'),
  			  ),
    		'pagination'=>array(
        	'pageSize'=>10,
    		),
			));
		
		$dataBusiness = $dataBusiness->getData();
		*/
		//return $dataBusiness[0]['id'];				
		return null;
	}
	
	public function behaviors(){
          return array( 'CAdvancedArBehavior' => array(
            'class' => 'application.extensions.CAdvancedArBehavior'));
          }
          
	
}